#include <malloc.h>
#include <cstddef>
#include "gtest/gtest.h" //import gtest library
#include "list.h"

struct ListNode* Head = NULL;

struct ListNode* InitList(){
  for (int i = 0; i < 6; ++i) {
    struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
    node->next = NULL;
    node->val = i;

    if(NULL == Head){
      Head = node;
      Head->next = NULL;
    }else{
      node->next = Head;
      Head = node;
    }
  }
  return Head;
}

void PrintList(struct ListNode* head){
  struct ListNode* temp = head;
  int index = 0;
  while(temp != NULL){
    printf("index:%d value:%d\n",index++,temp->val);
    temp = temp->next;
  }
}

struct ListNode* reverseList(struct ListNode* head){
  struct ListNode* prev = NULL;
  struct ListNode* curr = head;

  while (curr != NULL){
    //record next node
    struct ListNode* next = curr->next;

    //set next of cur node is pre node
    curr->next = prev;

    prev = curr;

    //step curr to next node
    curr = next;
  }

  //at the end,curr is NULL,prev is the last node.
  return prev;
}

TEST(TestReverseList, ReverseList){
  InitList();
  PrintList(Head);
  printf("--------------After Reverse List-------------------\n");
  struct ListNode* newHead = reverseList(Head);
  PrintList(newHead);
}
